package it.polimi.swKnights.SWIMv2.servlets.search;

import it.polimi.swKnights.SWIMv2.ejb.searchEngine.NormalSearchRemote;
import it.polimi.swKnights.SWIMv2.servlets.SwimServlet;

import java.io.IOException;
import java.util.ArrayList;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class NormalSearchProceed
 */
public class NormalSearchProceed extends SwimServlet
{
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{


		String query = (String) request.getAttribute("query");
		query = request.getParameter("query");

		if (query == null || query.length() < 3)
		{
			request.getRequestDispatcher("./normalSearch").forward(request,
					response);
			return;
		}

		List<Map<String, String>> mapValuesUser = new ArrayList<Map<String, String>>();

		NormalSearchRemote normalSR = this.lookUp(NormalSearchRemote.class);

		mapValuesUser = normalSR.lookFor(query);

		request.setAttribute("query", query);
		request.setAttribute("userResultMap", mapValuesUser);
		request.getRequestDispatcher("./normalSearch").forward(request,
				response);

	}

}
